<template>
  <div class="text">修改密码</div>
  <el-form 
    ref="formRef" 
    style="max-width: 600px" 
    :model="formData" 
    label-width="100px"
    class="demo-dynamic"
  >
    <el-form-item 
      prop="oldPwd" 
      label="原密码" 
      :rules="[
        {
          required: true,
          message: '请输入老密码',
          trigger: ['blur', 'change'],
        },
      ]"
    >
      <el-input  v-model="formData.oldPwd"/> 
    </el-form-item>
    <el-form-item 
      prop="newPwd" 
      label="新密码" 
      :rules="[
        {
          required: true,
          message: '请输入新密码',
          trigger: 'blur',
        },
        {
          min: 6,
          message: '密码长度至少6个字符',
          trigger: ['blur', 'change'],
        },
      ]"
    >
      <el-input type="password" show-password   v-model="formData.newPwd" />
    </el-form-item>
    <el-form-item 
      prop="confirmPwd" 
      label="确认新密码" 
      :rules="[
        {
          required: true,
          message: '请确认密码',
          trigger: 'blur',
        },
        {
          min: 6,
          message: '密码长度至少6个字符',
          trigger: ['blur', 'change'],
        },
        {
          validator: (rule, value, callback) => {
            if (value !== formData.newPwd) {
              callback(new Error('两次输入的密码不一致'));
            } else {
              callback();
            }
          },
          trigger: 'blur'
        }
      ]"
    >
      <el-input type="password" show-password  v-model="formData.confirmPwd"/>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm(formRef)">立即修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import { editpwd } from '@/api/account' 

const formRef = ref<FormInstance>()

const formData = reactive({
  oldPwd: '',
  newPwd: '',
  confirmPwd: ''
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      // 从本地存储获取用户信息
      const userInfo = JSON.parse(sessionStorage.getItem('user'))
     
      // 构造请求参数，注意要与接口文档要求一致
      const params = {
        oldPwd: formData.oldPwd,
        newPwd: formData.newPwd,
        newPwd1: formData.confirmPwd, 
        id: userInfo.id 
      }
      editpwd(params).then((res) => {
        if (res.code === 0) {
          ElMessage.success(res.msg)
          
          setTimeout(()=>{
             window.location.href = './Login';
          },1000)
        
        } else {
          ElMessage.error(res.msg)
        }
      }).catch((error) => {
        console.error('修改密码失败:', error)
        ElMessage.error('修改密码失败，请检查网络或联系管理员')
      })
    } else {
      console.log('error submit!')
    }
  })
}
</script>

<style scoped>
.text { 
    font-size: 20px;
    width: 100%;
    height: 50px;
    border-bottom: 2px solid #f0f2f5;
    line-height: 50px;
    margin-bottom: 20px;
}
</style>    